import { fileURLToPath, URL } from 'node:url';

import { defineConfig, loadEnv } from 'vite';
import vue from '@vitejs/plugin-vue';
import vueJsx from '@vitejs/plugin-vue-jsx';
import vueDevTools from 'vite-plugin-vue-devtools';

// https://vite.dev/config/
export default ({ mode }) => {
  const { VITE_PORT, VITE_PROXY_DOMAIN_REAL, VITE_PROXY_DOMAIN } = loadEnv(mode, process.cwd());
  return defineConfig({
    publicPath: process.env.NODE_ENV === 'local' ? '/' : './',
    plugins: [vue(), vueJsx(), vueDevTools()],
    resolve: {
      alias: {
        '@': fileURLToPath(new URL('./src', import.meta.url)),
      },
    },
    server: {
      open: true,
      port: VITE_PORT,
      cors: true,
      proxy: {
        [VITE_PROXY_DOMAIN]: {
          target: VITE_PROXY_DOMAIN_REAL,
          changeOrigin: true,
        },
      },
    },
    css: {},
  });
};
